import jwt
from datetime import datetime,timezone,timedelta
from fastapi.security import OAuth2PasswordBearer
from fastapi import HTTPException,Depends
from fastapi.responses import RedirectResponse
from datetime import datetime, timedelta,timezone
secret_key = "5f05aae111567a7c90267db28523d934"
oauth2_scheme = OAuth2PasswordBearer(tokenUrl='/login')
def create_token(data):
    data.update({"exp":datetime.now(timezone.utc) + timedelta(minutes=60)})
    encode_jwt = jwt.encode(data,key= secret_key, algorithm="HS256")
    return encode_jwt
def verify_token(token:str = Depends(oauth2_scheme)):
    if not token:
        return RedirectResponse(url="/login", status_code=302)
    try:
        decode_jwt = jwt.decode(token,key= secret_key, algorithms="HS256")#{'username':'admin','exp':1757146922},
        return decode_jwt['username']
    except Exception:
        return False
        # return RedirectResponse(url='/login',status_code=302)
